home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 700 < prev    next >
Internet Message Format  |  1996-08-06  |  4KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Restrictions on qsort compare function?
  5. Date: Fri, 05 Apr 96 17:49:42 GMT
  6. Organization: none
  7. Message-ID: <828726582snz@genesis.demon.co.uk>
  8. References: <4iokop$h4p@lyra.csx.cam.ac.uk> <4iqjar$2m9@usenet.pa.dec.com> <4jgltp$f9l@lyra.csx.cam.ac.uk> <828644274snz@genesis.demon.co.uk> <4k28t4$2g0@sam.inforamp.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4k28t4$2g0@sam.inforamp.net>
  15.            pcurran@inforamp.net "Peter Curran" writes:
  16.  
  17. >On Thu, 04 Apr 96 18:57:54 GMT in article <828644274snz@genesis.demon.co.uk>
  18. >    Lawrence Kirby <fred@genesis.demon.co.uk> (Lawrence Kirby) wrote:
  19. >
  20. >>In article <4jgltp$f9l@lyra.csx.cam.ac.uk>
  21. >>           jkb@mrc-lmb.cam.ac.uk "James Bonfield" writes:
  22. >
  23. >>>I now agree that non antisymmetric or nontransitive sort comparison functions
  24. >>>are indeed invalid. I can see how this can be construed from the ansi
  25. >>>standard, but can also see how it's possible to construe otherwise.
  26. >
  27. >>I don't. 7.10.5.2:
  28. >
  29. >>"The contents of the array are sorted into ascending
  30. >> order according to a comparison function pointed to by compar". 
  31. >
  32. >>If the comparison function produces inconsistent results then it is at odds
  33. >>with that sentence. At best that sentence has no well-defined meaning and
  34. >>the 'sort' operation as a whole has undefined behaviour.
  35. >
  36. ><snip>
  37. >
  38. >From the standard:
  39. >>"The function shall return an integer less than, equal to, or greater than
  40. >> zero if the first argument is considered to be respectively less than, equal
  41. >> to, or greater than the second."
  42. >
  43. >Again, agreeing that the intent was that the comparison function be consistent,
  44. >this statement does not require it.  (Actually, consistent is not quite the
  45. > word
  46. >I mean here - it must be consistent with a well-specified definition  - that is
  47. >implied by the first quote, above -  but that definition need not lead to a
  48. >comparison function that produces the same result for the same values on
  49. >successive calls, or follows other "sensible" patterns.)
  50.  
  51. It *must*  lead to a comparison that produces the same result for the same
  52. values on successive calls or else the term 'sort' is not applicable and
  53. qsort() has undefined behaviour.
  54.  
  55. >There is nothing here that demands that what one "considers to be ... less
  56. >than", etc., remain static over the duration of a call to qsort().  As I
  57. >suggested earlier, one could define a ordering that is time-dependent, or
  58. >changes in some other way between calls to the comparison function.  I see
  59. >nothing here that precludes a comparison function from then implementing such a
  60. >definition.
  61.  
  62. Such a comparison function is not consistent with sorting.
  63. It is not the job of the C standard to define basic computer science terms.
  64. Sorting has a well defined meaning and your comparison function is
  65. inconsistent with that meaning.
  66.  
  67. > As long as the comparison function produces results match the
  68. > rules
  69. >that have been established for considering values to be less than, etc., each
  70. >other each time it is called, whether those rules are static or not,, the
  71. >requirements of the standard have been met.
  72.  
  73. Undefined behaviour occurs where no behaviour is defined (3.16) i.e. you
  74. don't need to violate an explicit requirement in the standard to invoke it.
  75.  
  76. >This is clearly "language lawyering" of the worst kind.  Anyone who writes such
  77. >a comparison function will almost certainly get a well-earned mess.  However, I
  78. >see nothing in the standard that contradicts this interpretation.  For logical
  79. >correctness, I think the definition of the comparison function needs to be
  80. >tightened, although its hardly of pressing urgency.)
  81.  
  82. If you can find any definition as to what the behaviour should be with your
  83. comparison function, explain what it is. Otherwise the behaviour is
  84. undefined.
  85.  
  86. -- 
  87. -----------------------------------------
  88. Lawrence Kirby | fred@genesis.demon.co.uk
  89. Wilts, England | 70734.126@compuserve.com
  90. -----------------------------------------
  91.